home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume8 / getpw < prev    next >
Encoding:
Internet Message Format  |  1987-02-26  |  4.5 KB

  1. Subject:  v08i080:  Public-domain getpw*(3) routines
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: emoryu1!emoryu2!arnold (Arnold D. Robbins)
  6. Mod.sources: Volume 8, Issue 80
  7. Archive-name: getpw
  8.  
  9. Here is public domain re-implementation of the getpwent(3) routines. I have
  10. not included a manual page, since every Unix system has one. I also haven't
  11. even bothered to include a <pwd.h> file; you should be able to use the one
  12. on your system.
  13.  
  14. There is one additional routine: 
  15.     setpwfile (file)
  16.     char *file;
  17. which will cause the routines to find password records from a file besides
  18. /etc/passwd.  This is useful should you need to use saved password files,
  19. for instance in doing Unix accounting, where you wish to keep info around on
  20. old accounts, but take the old accounts out of the live password file.
  21. (Can you guess why I just whipped these up?)
  22.  
  23. To switch files, call setpwfile as the very first thing, or call endpwent(),
  24. then setpwfile ("/some/file").
  25.  
  26. Anyway, I hope this is useful to somene out there.
  27.  
  28. Arnold Robbins
  29. arnold@emoryu1.{CSNET, UUCP, ARPA, BITNET}
  30. #! /bin/sh
  31. # This is a shell archive.  Remove anything before this line,
  32. # then unpack it by saving it in a file and typing "sh file".
  33. # If all goes well, you will see the message "End of shell archive."
  34. # Contents:  getpw.c
  35. PATH=/bin:/usr/bin:/usr/ucb; export PATH
  36. echo shar: extracting "'getpw.c'" '(2658 characters)'
  37. if test -f 'getpw.c' ; then 
  38.   echo shar: will not over-write existing file "'getpw.c'"
  39. else
  40. sed 's/^X//' >getpw.c <<'@//E*O*F getpw.c//'
  41. X#include <stdio.h>
  42. X#include <pwd.h>
  43. X
  44. Xstatic char *pwdfile = "/etc/passwd";    /* default passwd file */
  45. Xstatic FILE *fp = NULL;
  46. Xstatic struct passwd curentry;        /* static data to return */
  47. X
  48. Xvoid setpwfile (cp)
  49. Xchar *cp;
  50. X{
  51. X    pwdfile = cp;
  52. X}
  53. X
  54. Xint setpwent ()
  55. X{
  56. X    if (fp)
  57. X        rewind (fp);
  58. X    else if ((fp = fopen (pwdfile, "r")) == NULL)
  59. X    {
  60. X#ifdef VERBOSE
  61. X        fprintf (stderr,
  62. X            "setpwent: %s non-existant or unreadable.\n", pwdfile);
  63. X#endif
  64. X        return (0);
  65. X    }
  66. X    return (1);
  67. X}
  68. X
  69. Xint endpwent ()
  70. X{
  71. X    if (fp)
  72. X    {
  73. X        fclose (fp);
  74. X        fp = NULL;
  75. X    }
  76. X    return 1;
  77. X}
  78. X
  79. Xstruct passwd *getpwent ()
  80. X{
  81. X    if (! fp && ! setpwent ())
  82. X        return (NULL);
  83. X
  84. X    if (! nextent ())
  85. X        return (NULL);
  86. X    else
  87. X        return (& curentry);
  88. X}
  89. X
  90. Xstruct passwd *getpwuid (uid)
  91. Xregister int uid;
  92. X{
  93. X    if (! setpwent ())
  94. X        return (NULL);
  95. X
  96. X    while (nextent ())
  97. X        if (curentry.pw_uid == uid)
  98. X            return (& curentry);
  99. X
  100. X    return (NULL);
  101. X}
  102. X
  103. Xstruct passwd *getpwnam (name)
  104. Xregister char *name;
  105. X{
  106. X    if (! setpwent ())
  107. X        return (NULL);
  108. X
  109. X    while (nextent ())
  110. X        if (strcmp (curentry.pw_name, name) == 0)
  111. X            return (& curentry);
  112. X
  113. X    return (NULL);
  114. X}
  115. X
  116. Xstatic char savbuf[BUFSIZ];
  117. X        
  118. Xstatic int nextent ()
  119. X{
  120. X    register char *cp;
  121. X
  122. X    if (! fp && ! setpwent ())
  123. X        return (0);
  124. X
  125. X    while (fgets (savbuf, sizeof(savbuf), fp) != NULL)
  126. X    {
  127. X        for (cp = savbuf; *cp && *cp != ':'; cp++)
  128. X            ;
  129. X        curentry.pw_name = savbuf;
  130. X        *cp++ = '\0';
  131. X        curentry.pw_passwd = cp;
  132. X        for (; *cp && *cp != ':'; cp++)
  133. X            ;
  134. X        *cp++ = '\0';
  135. X        curentry.pw_uid = atoi (cp);
  136. X        for (; *cp && *cp != ':'; cp++)
  137. X            ;
  138. X        *cp++ = '\0';
  139. X        curentry.pw_gid = atoi (cp);
  140. X        for (; *cp && *cp != ':'; cp++)
  141. X            ;
  142. X        *cp++ = '\0';
  143. X        curentry.pw_gecos = cp;
  144. X        for (; *cp && *cp != ':'; cp++)
  145. X            ;
  146. X        *cp++ = '\0';
  147. X        curentry.pw_dir = cp;
  148. X        for (; *cp && *cp != ':'; cp++)
  149. X            ;
  150. X        *cp++ = '\0';
  151. X        curentry.pw_shell = cp;
  152. X        for (; *cp && *cp != ':' && *cp != '\n'; cp++)
  153. X            ;
  154. X        *cp++ = '\0';
  155. X        return (1);
  156. X    }
  157. X    return (0);
  158. X}
  159. X
  160. X#ifdef TEST
  161. Xmain (argc, argv)
  162. Xint argc;
  163. Xchar **argv;
  164. X{
  165. X    struct passwd *pwd;
  166. X
  167. X    if (argc > 1)
  168. X        setpwfile (argv[1]);
  169. X
  170. X    setpwent ();
  171. X    while ((pwd = getpwent ()) != NULL)
  172. X    {
  173. X        printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  174. X            pwd->pw_name,
  175. X            pwd->pw_passwd,
  176. X            pwd->pw_uid,
  177. X            pwd->pw_gid,
  178. X            pwd->pw_gecos,
  179. X            pwd->pw_dir,
  180. X            pwd->pw_shell);
  181. X    }
  182. X    endpwent ();
  183. X
  184. X    if (pwd = getpwnam ("operator"))
  185. X        printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  186. X            pwd->pw_name,
  187. X            pwd->pw_passwd,
  188. X            pwd->pw_uid,
  189. X            pwd->pw_gid,
  190. X            pwd->pw_gecos,
  191. X            pwd->pw_dir,
  192. X            pwd->pw_shell);
  193. X
  194. X    if (pwd = getpwuid (1))
  195. X        printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  196. X            pwd->pw_name,
  197. X            pwd->pw_passwd,
  198. X            pwd->pw_uid,
  199. X            pwd->pw_gid,
  200. X            pwd->pw_gecos,
  201. X            pwd->pw_dir,
  202. X            pwd->pw_shell);
  203. X}
  204. X#endif
  205. @//E*O*F getpw.c//
  206. if test 2658 -ne "`wc -c <'getpw.c'`"; then
  207.     echo shar: error transmitting "'getpw.c'" '(should have been 2658 characters)'
  208. fi
  209. fi # end of overwriting check
  210. echo shar: "End of shell archive."
  211. exit 0
  212.